﻿using ExDUIR.NET.Declaration;
using ExDUIR.NET.Handle;
using ExDUIR.NET.Image;
using ExDUIR.NET.Canvas;
using System;

namespace ExDUIR.NET.ImageList
{
    public class ExImageList 
    {
        private readonly ExImageListHandle _handle;
        public IntPtr Handle => _handle.Handle;
        public bool IsValid => _handle.IsInvalid;
        internal ExImageList(ExImageListHandle handle)
        {
            _handle = handle;
        }

        public ExImageList(int nWidth, int nHeight)
        {
            _handle = new ExImageListHandle(ExAPI._imglist_create(nWidth, nHeight));
        }

        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="lpImage"></param>
        /// <param name="cbImage"></param>
        /// <param name="nIndex">位置索引,0为末尾</param>
        /// <returns>返回图片索引</returns>
        public int Add(byte[] lpImage, int cbImage, int nIndex)
        {
            return (int)ExAPI._imglist_add(_handle.Handle, (IntPtr)nIndex, lpImage, (IntPtr)cbImage);
        }

        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="image"></param>
        /// <param name="nIndex">位置索引,0为末尾</param>
        /// <returns>返回图片索引</returns>
        public int AddImage(ExImage image, int nIndex)
        {
            return (int)ExAPI._imglist_addimage(_handle.Handle, (IntPtr)nIndex, image.Handle);
        }

        public int Count
        {
            get
            {
                return ExAPI._imglist_count(_handle.Handle);
            }
        }

        public bool Del(int nIndex)
        {
            return ExAPI._imglist_del(_handle.Handle, (IntPtr)nIndex);
        }

        public bool Draw(int nIndex, ExCanvas canvas, int nLeft, int nTop, int nRight, int nBottom, int nAlpha)
        {
            return ExAPI._imglist_draw(_handle.Handle, (IntPtr)nIndex, canvas.Handle, nLeft, nTop, nRight, nBottom, nAlpha);
        }

        public ExImage Get(int nIndex)
        {
            return new ExImage(new ExImageHandle((int)ExAPI._imglist_get(_handle.Handle, (IntPtr)nIndex)));
        }

        public bool Set(int nIndex, byte[] lpImage, int cbImage)
        {
            return ExAPI._imglist_set(_handle.Handle, (IntPtr)nIndex, lpImage, (IntPtr)cbImage);
        }

        public bool SetImage(int nIndex, ExImage image)
        {
            return ExAPI._imglist_setimage(_handle.Handle, (IntPtr)nIndex, image.Handle);
        }

        public int Width
        {
            get
            {
                int width = 0, tmp = 0;
                ExAPI._imglist_size(_handle.Handle, out width, out tmp);
                return width;
            }
        }

        public int Height
        {
            get
            {
                int height = 0, tmp = 0;
                ExAPI._imglist_size(_handle.Handle, out tmp, out height);
                return height;
            }
        }

        public void Dispose() => _handle.Dispose();
    }
}
